/* eslint-disable import/prefer-default-export */
import React, { useEffect } from 'react';
import { connect } from 'umi';
import { get as getPath } from 'object-path';
import { Form, Input, Checkbox, Select, Spin, DatePicker, Button } from 'antd';
import moment from 'moment';

import { findUser } from '@/utils/dictionary';
import { FormContainer, SearchInput, SelectForm, RadioForm } from '@/components/UserForm';
import { getServicePersonlist, getemployeebyRole } from '@/services/attendance/overtime';
import { getTypeList } from '@/services/components/userform';
import styles from '../../RecruitMange.less';
import { findCountry } from './utils';

const { Item } = Form;
const { Option } = Select;
const { RangePicker } = DatePicker;

/** 招聘信息 */
const ZPXX = ({ form, disabled = true, formData }) => {
    const { getFieldDecorator } = form;
    const {
        companyName = '',
        applyDate = '',
        recruitPost = '',
        departmentName = '',
        projectGroup = '',
        totalNums = '',
        workNums = '',
        requireNums = '',
        entryDate = null,
        entryDateEnd = null,
        necessary = '',
        reason1 = '',
        reason2 = '',
        otherReason = '',
        workNature = '',
        postRank = null
        // workNatureOther = ''
    } = formData;
    const flagReason2 = form.getFieldValue('reason2') === 'supply_for_other';
    return (
        <div className="form-Wrap">
            <FormContainer>
                <Form.Item label="需求公司">
                    {getFieldDecorator('companyName', {
                        initialValue: companyName,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="申请日期">
                    {getFieldDecorator('applyDate', {
                        initialValue: moment(applyDate),
                        rules: [
                            {
                                // required: true,
                                // message: '请选择入库日期'
                            }
                        ]
                    })(<DatePicker style={{ width: '100%' }} disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="需求职位">
                    {getFieldDecorator('recruitPost', {
                        initialValue: recruitPost,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="职级">
                    {getFieldDecorator('postRank', {
                        initialValue: postRank
                    })(<Input disabled />)}
                </Form.Item>

                <Form.Item label="需求部门">
                    {getFieldDecorator('departmentName', {
                        initialValue: departmentName,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="项目组">
                    {getFieldDecorator('projectGroup', {
                        initialValue: projectGroup,
                        rules: [
                            {
                                // required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="定编人数">
                    {getFieldDecorator('totalNums', {
                        initialValue: totalNums,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="在职人数">
                    {getFieldDecorator('workNums', {
                        initialValue: workNums,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="需求人数">
                    {getFieldDecorator('requireNums', {
                        initialValue: requireNums,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<Input disabled={disabled} />)}
                </Form.Item>
                <Form.Item label="期望到岗时间">
                    {getFieldDecorator('expectTime', {
                        initialValue: entryDate ? [moment(entryDate), moment(entryDateEnd)] : null,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(<RangePicker disabled={disabled} style={{ width: '100%' }} />)}
                </Form.Item>
                <Form.Item label="需求等级">
                    {getFieldDecorator('necessary', {
                        initialValue: necessary,
                        rules: [
                            {
                                // required: true
                            }
                        ]
                    })(
                        <SelectForm
                            params={{ dictCode: 'necessaryLevel' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                selectValue: 'dictValue',
                                selectTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            selectProps={{
                                placeholder: '需求等级',
                                disabled
                            }}
                        />
                    )}
                </Form.Item>
                <Form.Item label="工作性质">
                    {getFieldDecorator('workNature', {
                        initialValue: workNature,
                        rules: []
                    })(
                        <RadioForm
                            params={{ dictCode: 'jobNature' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                radioValue: 'dictValue',
                                radioTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            radioProps={{
                                placeholder: '',
                                disabled
                            }}
                        />
                    )}
                </Form.Item>
            </FormContainer>
            <Form.Item
                label="增补原因"
                labelCol={{ md: 4 }}
                wrapperCol={{
                    md: flagReason2 ? 20 : 8
                }}
            >
                <span
                    style={{
                        display: 'inline-block',
                        width: flagReason2 ? '32%' : '49%'
                    }}
                >
                    {getFieldDecorator('reason1', {
                        initialValue: reason1,
                        rules: []
                    })(
                        <SelectForm
                            params={{ dictCode: 'supplyReasonLevel' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                selectValue: 'dictValue',
                                selectTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            selectProps={{
                                placeholder: '',
                                disabled
                            }}
                        />
                    )}
                </span>
                <span
                    style={{
                        display: 'inline-block',
                        width: flagReason2 ? '32%' : '49%',
                        marginLeft: '2%'
                    }}
                >
                    {getFieldDecorator('reason2', {
                        initialValue: reason2,
                        rules: [
                            {
                                required: true,
                                placeholder: '请选择'
                            }
                        ]
                    })(
                        <SelectForm
                            params={{
                                dictCode:
                                    form.getFieldValue('reason1') === 'temporaryNeed'
                                        ? 'temporaryNeedType'
                                        : 'scheduledNeedType'
                            }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                selectValue: 'dictValue',
                                selectTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            selectProps={{
                                placeholder: '请选择',
                                disabled
                            }}
                        />
                    )}
                </span>
                <span
                    style={{
                        display: 'inline-block',
                        width: flagReason2 ? '32%' : '0%',
                        marginLeft: '2%'
                    }}
                >
                    {flagReason2 &&
                        getFieldDecorator('otherReason', {
                            initialValue: otherReason,
                            rules: [
                                {
                                    required: true
                                }
                            ]
                        })(<Input disabled={disabled} />)}
                </span>
            </Form.Item>
        </div>
    );
};

/** 候选人要求 */
const HXRYQ = ({ form, disabled = true, formData }) => {
    const { getFieldDecorator } = form;
    const {
        requireDegree = '',
        requireSex = '',
        timeEmployed = '',
        salaryStart = '',
        salaryEnd = '',
        salaryMonth = '',
        tryContent = '',
        workContent = '',
        otherContent = ''
    } = formData;
    return (
        <div className="form-Wrap">
            <FormContainer>
                <Form.Item label="最低学历">
                    {getFieldDecorator('requireDegree', {
                        initialValue: requireDegree
                    })(
                        <SelectForm
                            params={{ dictCode: 'agreeType' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                selectValue: 'dictValue',
                                selectTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            selectProps={{
                                placeholder: '请选择',
                                disabled
                            }}
                        />
                    )}
                </Form.Item>
                <Form.Item label="性别">
                    {getFieldDecorator('requireSex', {
                        initialValue: requireSex,
                        rules: [
                            {
                                // required: true
                            }
                        ]
                    })(
                        <RadioForm
                            params={{ dictCode: 'candidateSexType' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                radioValue: 'dictValue',
                                radioTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            radioProps={{
                                placeholder: '',
                                disabled
                            }}
                        />
                    )}
                </Form.Item>
                <Form.Item label="工作年限">
                    {getFieldDecorator('timeEmployed', {
                        initialValue: timeEmployed
                    })(
                        <SelectForm
                            params={{ dictCode: 'timeEmployedType' }}
                            requireParam={['dictCode']}
                            getData={params =>
                                getTypeList(params).then(res => {
                                    if (res && res.code === 10000 && res.data) {
                                        return res.data || [];
                                    }
                                    return [];
                                })
                            }
                            fieldsName={{
                                selectValue: 'dictValue',
                                selectTitle: 'dictName'
                            }}
                            // onChange={this.companyChange}
                            selectProps={{
                                placeholder: '请选择',
                                disabled
                            }}
                        />
                    )}
                </Form.Item>
                <Form.Item label="薪资范围" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                    {getFieldDecorator('salaryStart', {
                        initialValue: salaryStart,
                        rules: [
                            {
                                // required: true
                            }
                        ]
                    })(
                        <Input
                            addonAfter="K/月"
                            disabled={disabled}
                            style={{ width: '32%', display: 'inline-block' }}
                        />
                    )}

                    <span
                        style={{
                            width: '2%',
                            display: 'inline-block',
                            textAlign: 'center'
                        }}
                    >
                        ~
                    </span>
                    {getFieldDecorator('salaryEnd', {
                        initialValue: salaryEnd,
                        rules: [
                            {
                                // required: true
                            }
                        ]
                    })(
                        <Input
                            addonAfter="K/月"
                            disabled={disabled}
                            style={{ width: '32%', display: 'inline-block' }}
                        />
                    )}
                    <span
                        style={{
                            width: '2%',
                            display: 'inline-block',
                            textAlign: 'center'
                        }}
                    >
                        X
                    </span>
                    {getFieldDecorator('salaryMonth', {
                        initialValue: salaryMonth,
                        rules: [
                            {
                                required: true
                            }
                        ]
                    })(
                        <Input
                            addonAfter="K/月"
                            disabled={disabled}
                            style={{ width: '32%', display: 'inline-block' }}
                        />
                    )}
                </Form.Item>
                <Form.Item label="职位工作内容" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                    {getFieldDecorator('workContent', {
                        initialValue: workContent
                    })(
                        <Input.TextArea
                            disabled={disabled}
                            rows={4}
                            style={{ 'word-break': ' break-all' }}
                            placeholder="请输入至少两个字符"
                        />
                    )}
                </Form.Item>
                <Form.Item label="试用期标准：" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                    {getFieldDecorator('tryContent', {
                        initialValue: tryContent
                    })(
                        <Input.TextArea
                            rows={4}
                            disabled={disabled}
                            style={{ 'word-break': ' break-all' }}
                            placeholder="请输入至少两个字符"
                        />
                    )}
                </Form.Item>
                <Form.Item label="其他要求" vvspan={24} labelCol={{ md: 4 }} wrapperCol={{ md: 20 }}>
                    {getFieldDecorator('otherContent', {
                        initialValue: otherContent
                    })(
                        <Input.TextArea
                            rows={4}
                            disabled={disabled}
                            style={{ 'word-break': ' break-all' }}
                            placeholder="请输入至少两个字符"
                        />
                    )}
                </Form.Item>
            </FormContainer>
        </div>
    );
};

/**
 * 招聘进度设置
 * form user
 */
const InnerZPJDSZ = props => {
    const {
        form,
        dispatch,
        loading,
        query,
        location: { pathname },
        formData,
        dictionary,
        editZPJD = false, // 是否编辑
        isModal = false, // 是否弹窗中编辑
        setEditZPJD
    } = props;
    const { ZPQD = 'ChinaRecruitWay' } = findCountry(query.companyId);
    const { isUser = false, isHR = false } = findUser(pathname);
    const { getFieldDecorator } = form;
    const { leaderIds = [], recruitSource, schedule, servicePersonId } = formData;
    useEffect(() => {
        const fetchWay = async () => {
            dispatch({
                type: 'dictionary/fetchDictionaryList',
                payload: {
                    codes: ['recruitScheduleType', ZPQD]
                }
            });
        };
        fetchWay();
    }, []);
    return (
        <Spin spinning={loading}>
            <div className={isModal ? '' : 'form-Wrap'}>
                <FormContainer>
                    <Item label="招聘进度">
                        {getFieldDecorator('schedule', {
                            initialValue: schedule,
                            rules: []
                        })(
                            <Select disabled={!editZPJD || isUser} style={{ width: '100%' }}>
                                {getPath(dictionary, 'recruitScheduleType', []).map(item => (
                                    <Option value={item.dictValue} key={item.dictValue}>
                                        {item.dictName}
                                    </Option>
                                ))}
                            </Select>
                        )}
                    </Item>
                    <Form.Item label="业务负责人">
                        {getFieldDecorator('servicePersonId', {
                            initialValue: servicePersonId === -1 ? undefined : servicePersonId,
                            rules: []
                        })(
                            <SearchInput
                                params={{
                                    companyId: query.companyId
                                }}
                                requireParam={['companyId']}
                                getData={params =>
                                    getServicePersonlist(params).then(res => {
                                        if (res && res.code === 10000) {
                                            return res.data || [];
                                        }
                                        return [];
                                    })
                                }
                                selectProps={{
                                    disabled: !editZPJD || isUser,
                                    placeholder: '请输入姓名',
                                    allowClear: true,
                                    style: { width: '100%' }
                                }}
                                fieldsName={{
                                    value: 'id',
                                    title: 'employeeName',
                                    extraParams: 'userName'
                                }}
                                // onChange={this.handleSelect}
                            />
                        )}
                    </Form.Item>

                    <Form.Item
                        label="招聘负责人"
                        vvspan={24}
                        labelCol={{ md: isModal ? 3 : 4 }}
                        wrapperCol={{ md: isModal ? 19 : 20 }}
                    >
                        {getFieldDecorator('leaderIds', {
                            initialValue: leaderIds
                        })(
                            <SearchInput
                                params={{
                                    companyId: query.companyId,
                                    roleName: '招聘负责人' // TODO： 后面服务端会给字典
                                }}
                                getData={params =>
                                    getemployeebyRole(params).then(res => {
                                        if (res && res.code === 10000) {
                                            return res.data || [];
                                        }
                                        return [];
                                    })
                                }
                                selectProps={{
                                    disabled: !editZPJD || isHR,
                                    style: { width: '100%' },
                                    placeholder: '请输入姓名',
                                    mode: 'multiple',
                                    maxTagCount: 3
                                }}
                                fieldsName={{
                                    value: 'id',
                                    title: 'employeeName',
                                    extraParams: 'employeeName',
                                    getemployeebyRole: true
                                }}
                            />
                        )}
                    </Form.Item>
                    <Form.Item
                        label="招聘渠道"
                        vvspan={24}
                        labelCol={{ md: isModal ? 3 : 4 }}
                        wrapperCol={{ md: isModal ? 19 : 20 }}
                    >
                        <div className={styles.vv_oa_checkbox}>
                            {getFieldDecorator('recruitSource', {
                                initialValue: recruitSource ? recruitSource.split(',') : [],
                                rules: []
                            })(
                                <Checkbox.Group style={{ width: '100%' }} disabled={!editZPJD || isUser}>
                                    {getPath(dictionary, ZPQD, []).map(item => (
                                        <Checkbox value={item.dictValue} key={item.dictValue}>
                                            {item.dictName}
                                        </Checkbox>
                                    ))}
                                </Checkbox.Group>
                            )}
                        </div>
                    </Form.Item>
                    {!isModal && editZPJD && (
                        <Form.Item label=" " className={styles.nullFormItem}>
                            <Button type="primary" htmlType="submit">
                                提交
                            </Button>
                            <Button
                                style={{ marginLeft: 15 }}
                                onClick={() => {
                                    form.resetFields();
                                    setEditZPJD(false);
                                }}
                            >
                                取消
                            </Button>
                        </Form.Item>
                    )}
                </FormContainer>
            </div>
        </Spin>
    );
};
const mapStateToProps = ({ dictionary, loading }) => ({
    dictionary,
    loading: loading.models.dictionary
});
const ZPJDSZ = connect(mapStateToProps)(InnerZPJDSZ);
export { ZPXX, HXRYQ, ZPJDSZ };
